﻿using CatUtils.Utils.Currency;
using System;
using System.IO;
using System.Net;
using System.Text;

namespace CatUtilsDll.Utils.Currency.WebUtils
{
    public partial class WebHelper
    {
        #region Http方法

        /// <summary>
        /// 同步获取html代码
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <returns></returns>
        public static string HttpPcGet(string RequestUrl)
        {
            return HttpGet(RequestUrl, PcWebHeader(RequestUrl));
        }

        /// <summary>
        /// 同步获取html代码
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="encoding">获取时使用的编码格式</param>
        /// <returns></returns>
        public static string HttpPcGet(string RequestUrl, Encoding encoding)
        {
            return HttpGet(RequestUrl, PcWebHeader(RequestUrl), encoding);
        }

        /// <summary>
        /// 同步获取html代码,使用UTF8编码
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="webHeaderCollection"></param>
        /// <returns></returns>
        public static string HttpGet(string RequestUrl, WebHeaderCollection webHeaderCollection)
        {
            return HttpGet(RequestUrl, webHeaderCollection, Encoding.UTF8);
        }

        /// <summary>
        /// 同步获取html代码
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="webHeaderCollection"></param>
        /// <param name="encoding">获取时使用的编码格式</param>
        /// <returns></returns>
        public static string HttpGet(string RequestUrl, WebHeaderCollection webHeaderCollection, Encoding encoding)
        {
            string HtmlCode = "";

            using (WebClient webClient = new WebClient())
            {
                try
                {
                    webClient.Headers = webHeaderCollection;

                    HtmlCode = new StreamReader(webClient.OpenRead(RequestUrl), encoding).ReadToEnd();
                }
                catch (WebException exception)
                {
                    string responseText = "";

                    var responseStream = exception.Response?.GetResponseStream();

                    if (responseStream != null)
                    {
                        using (var reader = new StreamReader(responseStream))
                        {
                            responseText = reader.ReadToEnd();
                        }
                    }

                    PrintWebLog(RequestUrl, exception);
                    return responseText;
                }
                catch (Exception ex)
                {
                    PrintWebLog(RequestUrl, ex);
                }
            }
            return HtmlCode;
        }

        /// <summary>
        /// 同步获取html代码
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <returns></returns>
        public static string MobileGet(string RequestUrl)
        {
            return HttpGet(RequestUrl, MobileWebHeader(RequestUrl));
        }

        #endregion Http方法

        #region Post方法

        /// <summary>
        /// 发送Post请求并返回结果，使用UTF8
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="PostData"></param>
        /// <returns></returns>
        public static string HttpPost(string RequestUrl, string PostData)
        {
            WebHeaderCollection webHeaderCollection = WebHelper.PcWebHeader(RequestUrl);

            webHeaderCollection.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            return HttpPost(RequestUrl, PostData, webHeaderCollection);
        }

        /// <summary>
        ///发送Post请求并返回结果，使用UTF8  可自定义请求头
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="PostData"></param>
        /// <param name="webHeaderCollection"></param>
        /// <returns></returns>
        public static string HttpPost(string RequestUrl, string PostData, WebHeaderCollection webHeaderCollection)
        {
            return HttpPost(RequestUrl, PostData, webHeaderCollection, Encoding.UTF8);
        }

        /// <summary>
        /// 发送Post请求并返回结果
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="PostData"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string HttpPost(string RequestUrl, string PostData, Encoding encoding)
        {
            return HttpPost(RequestUrl, PostData, PcWebHeader(), encoding);
        }

        /// <summary>
        /// 发送Post请求并返回结果  可自定义请求头
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="PostData"></param>
        /// <param name="webHeaderCollection"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string HttpPost(string RequestUrl, string PostData, WebHeaderCollection webHeaderCollection, Encoding encoding)
        {
            using (WebClient PostWebClient = new WebClient())
            {
                PostWebClient.Headers = webHeaderCollection;

                try
                {
                    byte[] byteArray = encoding.GetBytes(PostData);

                    byte[] responseArray = PostWebClient.UploadData(RequestUrl, byteArray);

                    return Encoding.UTF8.GetString(responseArray);
                }
                catch (WebException exception)
                {
                    string responseText = "";

                    var responseStream = exception.Response?.GetResponseStream();

                    if (responseStream != null)
                    {
                        using (var reader = new StreamReader(responseStream))
                        {
                            responseText = reader.ReadToEnd();
                        }
                    }
                    PrintWebLog(RequestUrl, exception);
                    return responseText;
                }
                catch (Exception ex)
                {
                    PrintWebLog(RequestUrl, ex);
                }
            }
            return "";
        }

        #endregion Post方法

        /// <summary>
        /// 调用HTTP Delete方法
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="Data"></param>
        /// <param name="webHeader"></param>
        /// <returns></returns>
        public static string HttpDelete(string RequestUrl, string Data, WebHeaderCollection webHeader)
        {
            return HttpMethod(RequestUrl, "DELETE", Data, webHeader);
        }

        /// <summary>
        /// 调用HTTP PATCH方法
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="Data"></param>
        /// <param name="webHeader"></param>
        /// <returns></returns>
        public static string HttpPatch(string RequestUrl, string Data, WebHeaderCollection webHeader)
        {
            return HttpMethod(RequestUrl, "PATCH", Data, webHeader);
        }

        /// <summary>
        /// 调用HTTP PUT方法
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="Data"></param>
        /// <param name="webHeader"></param>
        /// <returns></returns>
        public static string HttpPUT(string RequestUrl, string Data, WebHeaderCollection webHeader)
        {
            return HttpMethod(RequestUrl, "PUT", Data, webHeader);
        }

        /// <summary>
        /// 用WebClient发送修改Method的请求，有delete
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="Method"></param>
        /// <param name="Data"></param>
        /// <param name="webHeader"></param>
        /// <returns></returns>
        public static string HttpMethod(string RequestUrl, string Method, string Data, WebHeaderCollection webHeader)
        {
            try
            {
                using (var client = new WebClient())
                {
                    client.Headers = webHeader;
                    return client.UploadString(RequestUrl, Method, Data);
                }
            }
            catch (WebException exception)
            {
                string responseText = "";

                var responseStream = exception.Response?.GetResponseStream();

                if (responseStream != null)
                {
                    using (var reader = new StreamReader(responseStream))
                    {
                        responseText = reader.ReadToEnd();
                    }
                }
                PrintWebLog(RequestUrl, exception);
                return responseText;
            }
            catch (Exception ex)
            {
                PrintLog.E(ex);
            }
            return "";
        }
    }
}